package net.sppan.blog.service.impl;

import java.util.Date;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import net.sppan.blog.entity.Check;
import net.sppan.blog.exception.ServiceException;
import net.sppan.blog.repository.CheckRepository;
import net.sppan.blog.service.CheckService;
@Service
@Transactional
public class CheckServiceImpl implements CheckService{
	@Resource
	private CheckRepository checkRepository;
	@Override
	public Page<Check> findAll(PageRequest pageRequest) {
		return checkRepository.findAll(pageRequest);
	}
	@Override
	public Check findById(Long id) {
		Check check = checkRepository.findOne(id);
		checkRepository.saveAndFlush(check);
		return check;
	}
	@Override
	public void saveOrUpdate(Check check) {
		if(check == null){
			throw new ServiceException("操作对象不能为空");
		}
		
		if(check.getId() != null){
			Check dbcheck = findById(check.getId());
			dbcheck.setTitle(check.getTitle());
			dbcheck.setContent(check.getContent());
			dbcheck.setFile_ids(check.getFile_ids());
			dbcheck.setNickName(check.getNickName());
			dbcheck.setHeadFile(check.getHeadFile());
			dbcheck.setSuccess(check.getSuccess());
			checkRepository.saveAndFlush(dbcheck);
		}else{
			//设置博客基本属性
			check.setCreateAt(new Date());
			checkRepository.save(check);	
		}
		
	}
	@Override
	public void change(Long id, String type) {
		Check blog = findById(id);
		
		checkRepository.save(blog);
	}

	@Override
	public void delete(Long id) {
		checkRepository.delete(id);
	}
	@Override
	public Page<Check> findPrivacy(int i, Pageable pageable) {
		
		return checkRepository.findAll(pageable);
	}

}
